clc;
clearvars;
%% Load or Make Data
TempDataFile='Data\PerformanceData_NoFS_NoB.mat';
ReplaceChaseFlag=true;
if exist(TempDataFile,'file')
    Yes_NO = questdlg({'Cash File Exist','Replace it?'});
    if strcmp(Yes_NO,'No') || strcmp(Yes_NO,'Cancel')
        ReplaceChaseFlag=false;
    end
end
if ReplaceChaseFlag
RNGState=rng('shuffle');
K=68;
[Train,Test]=GetDataFromRepository(true,1);

FeaturesIndex={ [1:5,8:11,28,29] %Subset1 Just PStay
                [1:5,8:11,28,29,30:39]}; % Subset1+2 All Features Included
KNNNum=length(FeaturesIndex);
EstimatedValue=zeros(Test.SNum,KNNNum);
for i=1:KNNNum
fprintf('Analyze Feature for Index # %d  of  %d .\n',i,KNNNum)
KNNRegressor = KNNReg(Train.Features(:,FeaturesIndex{i}),Train.Label,'NumNeighbors',K,'RegresiorMethod','InverseDistance');
EstimatedValue(:,i)=GetRegValue(KNNRegressor,Test.Features(:,FeaturesIndex{i}));
end
save(TempDataFile,'Test','EstimatedValue','KNNNum','RNGState')
else
load(TempDataFile)
end
FitNum=size(Test.BestFittedW,2);
%% Extract Error MAE and STD
MAE=zeros(KNNNum+FitNum,1);
STD=zeros(KNNNum+FitNum,1);
R=zeros(KNNNum+FitNum,1);
for i=1:KNNNum
    Erorrs=EstimatedValue(:,i)-Test.Label;
    Erorrs(isnan(Erorrs))=[];
    AE = abs(Erorrs);
    MAE(i)=mean(AE);
    STD(i)=std(AE);
    R(i)=corr(EstimatedValue(:,i),Test.Label);
end
for i=KNNNum+1:KNNNum+FitNum
    Erorrs=Test.BestFittedW(:,i-KNNNum)-Test.Label;
    Erorrs(isnan(Erorrs))=[];
    AE = abs(Erorrs);
    MAE(i)=mean(AE);
    STD(i)=std(AE);
    R(i)=corr(Test.BestFittedW(:,i-KNNNum),Test.Label);
end
%% Plots Options
BasePlotFileName='Plots\Performance_UFS_NoB_Fig';
TextSize=14;
SavePlots=1;
MaxC=0.3;
Titles = {'\itk-nn (\wp_{1})';
          '\itk-nn (\wp_{1+2})';
          'ML' ;
          'MAP'       };


YLabels= {  '$\hat{w}_{\it{k}-nn 1}$' ;
            '$\hat{w}_{\it{k}-nn 1+2}$';
            '$\hat{w}_{ML}$';
            '$\hat{w}_{MAP}$'      };

H=[0.5:0.001:0.6,0.6:0.0015:0.8,0.8:0.00040:1,0:0.000345:0.16];
N=length(H);
MAP=hsv2rgb([H',ones(N,1),ones(N,1)]);
% Fig 1 Plot Scatter and Parzen estimation
Fig.Fig=1;
Fig.XLim=[0,1];
Fig.YLim=[0,1];
Fig.AddLine=false;
Fig.TypeValues=false;
Fig.ParzenH=0.04;
Fig.TextSize=TextSize;
Fig.ColorMAP=MAP;
Fig.ColorRenge=[0,MaxC];
Fig.ColorBar=false;

Fig.SubFig={2,2,1};
Fig.Title=Titles{1};
Fig.YLabel=YLabels{1};
PlotPerformance(Test.Label,EstimatedValue(:,1),Fig);

Fig.SubFig={2,2,2};
Fig.Title=Titles{2};
Fig.YLabel=YLabels{2};
PlotPerformance(Test.Label,EstimatedValue(:,2),Fig);

Fig.SubFig={2,2,3};
Fig.Title=Titles{3};
Fig.YLabel=YLabels{3};
PlotPerformance(Test.Label,Test.BestFittedW(:,1),Fig);

Fig.SubFig={2,2,4};
Fig.Title=Titles{4};
Fig.YLabel=YLabels{4};
PlotPerformance(Test.Label,Test.BestFittedW(:,2),Fig);

%
set(gcf,'Position',[10,50,850,650]);

subplot('Position',[0.92,0.1,0.2,0.8]);
set(gca,'visible','off');
caxis([0,MaxC])
colormap(MAP)
colorbar('Location','west','position',[0.95,0.1,0.04,0.8],'Ticks',0:0.1:1,'FontSize',TextSize-2);
set(gca,'FontSize',TextSize)
subplot(2,2,2)
text(1.04,1.04,'P(Real w|w)','FontSize',TextSize,'HorizontalAlignment','center','VerticalAlignment','middle')
%
if SavePlots
FileName=[BasePlotFileName,'0'];
saveas(gcf,[FileName,'.jpg']);
saveas(gcf,[FileName,'.emf']);
end
%% Extract Error Histograms
Bins=-1:0.1:1;
% B=[-1,-0.9 -0.8 -0.7 -0.6 -0.5 -0.3 -0.15];
% Bins=[B,0,-B(end:-1:1)];
BinNum=length(Bins);
Hist=zeros(BinNum,KNNNum+FitNum);
for i=1:KNNNum
    Erorrs=EstimatedValue(:,i)-Test.Label;
    Erorrs(isnan(Erorrs))=[];
    Hist(:,i)=hist(Erorrs,Bins);
end
for i=KNNNum+1:KNNNum+FitNum
    Erorrs=Test.BestFittedW(:,i-KNNNum)-Test.Label;
    Erorrs(isnan(Erorrs))=[];
    Hist(:,i)=hist(Erorrs,Bins);
end

% Plot Probability of Errors
figure(6);clf
Hist(Hist==0)=1e-7;
plot(Bins,Hist/Test.SNum,'LineWidth',3)

set(gca,'FontSize',TextSize)
set(gca,'YScale','log')
xlim([-1.1,1.1])
ylim([5e-5,1])
% xticks(0:0.2:1);
% xtickangle(90);
legend(Titles,'location','NorthEast')
xlabel('Error =$\hat{w}-w_{o}$','Interpreter','latex');
ylabel('Probability of Error','Interpreter','latex');
set(gcf,'Position',[50,50,1200,500]);
if SavePlots
FileName=[BasePlotFileName,'2.3_AbsErrorsPlotNew_Fig8'];
saveas(gcf,[FileName,'.jpg']);
saveas(gcf,[FileName,'.emf']);
end

%%
if SavePlots
FileName=[BasePlotFileName,'2_ErrorsHist_Fig8'];
saveas(gcf,[FileName,'.jpg']);
saveas(gcf,[FileName,'.emf']);
end
%%
ZeroError=find(Bins==0);
Values=Hist(ZeroError,:)/Test.SNum;
%%

clear Fig
Fig.Fig=5;
Fig.XLim=[0,1];
Fig.YLim=[-0.02,1.0215];
Fig.AddLine=false;
Fig.TextSize=13;
Fig.AreaT=0.1;
Fig.AreaA=(1-Fig.AreaT)/2;
Fig.AreaC=0.02;
Fig.CAreaPercentageTreshold=0;
Fig.XLabel='$w_{o}$';

Fig.SubFig={3,2,1};
Fig.Title=Titles{1};
Fig.YLabel=YLabels{1};
PlotPerformanceBox(Test.Label,EstimatedValue(:,1),Fig);

Fig.SubFig={3,2,2};
Fig.Title=Titles{2};
Fig.YLabel=YLabels{2};
PlotPerformanceBox(Test.Label,EstimatedValue(:,2),Fig);

Fig.SubFig={3,2,3};
Fig.Title=Titles{3};
Fig.YLabel=YLabels{3};
PlotPerformanceBox(Test.Label,Test.BestFittedW(:,1),Fig);

Fig.SubFig={3,2,4};
Fig.Title=Titles{4};
Fig.YLabel=YLabels{4};
PlotPerformanceBox(Test.Label,Test.BestFittedW(:,2),Fig);

% Legend
Fig.SubFig={6,2,9:10};
Fig.Title='';
PlotPerformanceBoxLegendV3(Fig);
%
set(gcf,'Position',[10,10,650,1450]);
%%
if SavePlots
FileName=[BasePlotFileName,'3_Fig7'];
saveas(gcf,[FileName,'.jpg']);
saveas(gcf,[FileName,'.emf']);
end

%%
% display Table 
table(MAE,STD,R,'RowNames',Titles)
